package com.share.example.selenium.config;

import com.share.common.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.NoSuchWindowException;
import org.openqa.selenium.WebDriverException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常拦截器
 */

@Slf4j
@ControllerAdvice
public class GlobalException {

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Object handleException(Exception e) {
        String message = e.getMessage();
        if (e instanceof NoSuchWindowException) {
            log.error("selenium打开的窗口被关闭了", e);
            System.exit(-1);
            return Result.failure("selenium打开的窗口被关闭了" + message);
        } else if (e instanceof WebDriverException) {
            log.error("WebDriverException拦截异常", e);
            return Result.failure("WebDriverException拦截异常" + message);
        } else {
            log.error("全局异常拦截器-接口调用异常!!!", e);
            return Result.failure("服务异常,请稍后重试!!!");
        }
    }
}
